package IOStream.Exercise001;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * ClassName:Exercise006
 * Package:IOStream.Exercise
 * Description:
 *
 * @Author:ZhangXin
 * @Create2023/3/2-20:37
 * @Version:v1.0
 */
public class Exercise006 {
    /**
     * 修改文本顺序
     * 需求：把<出师表>的文章顺序进行恢复到一个新的文件中
     */
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("E:\\aaa\\csb.txt"));
        ArrayList<String> list = new ArrayList<>();
        String line;
        while ((line = br.readLine()) != null) {
            list.add(line);
        }
        br.close();
        //排序
        //排序规则：按照前面的序号进行排序
        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                //获取o1和o2的序号
                int i1 = Integer.parseInt(o1.split("\\.")[0]);
                int i2 = Integer.parseInt(o2.split("\\.")[0]);
                return i1-i2;
            }
        });

        //写出
        BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\aaa\\csbCopy.txt"));
        for (String str : list) {
            bw.write(str);
            bw.newLine();//换行
        }
        bw.close();
    }
}
